package com.xy.Servlet;

import java.io.IOException;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xy.model.BJi;
import com.xy.model.Category;
import com.xy.model.Level;
import com.xy.model.Major;
import com.xy.model.PackageBook;
import com.xy.model.Student;
import com.xy.model.TextBook;
import com.xy.service.PackageBookService;
import com.xy.service.impl.CategoryServiceImpl;
import com.xy.service.impl.ClassServiceImpl;
import com.xy.service.impl.LevelServiceImpl;
import com.xy.service.impl.MajorServiceImpl;
import com.xy.service.impl.PackageBookServiceImpl;
import com.xy.service.impl.StudentServiceImpl;
import com.xy.service.impl.TextBookServiceImpl;

/**
 * Servlet implementation class PackageBookServlet
 */
@WebServlet("/packageBookServlet")
public class PackageBookServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PackageBookServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		PackageBookService service = new PackageBookServiceImpl();
		
		if ("add".equals(request.getParameter("status"))) {
			List<TextBook> tbs = (List<TextBook>) request.getSession().getAttribute("tbs");
			Set<TextBook> tbss = new HashSet<TextBook>();

			PackageBook pb = new PackageBook();
			String academicYear = request.getParameter("academicYear");
			String semester = request.getParameter("semester");
			String category = request.getParameter("category");
			String major = request.getParameter("major");
			String level = request.getParameter("level");
			String grade = request.getParameter("grade");
			pb.setAcademyYear(academicYear);
			pb.setSemester(semester);
			pb.setCategory((Category) new CategoryServiceImpl().get(Integer.valueOf(category)));
			pb.setMajor((Major) new MajorServiceImpl().get(Integer.valueOf(major)));
			pb.setLevel((Level) new LevelServiceImpl().get(Integer.valueOf(level)));
			pb.setCreateDate(new Date());
			pb.setGrade(grade);
			for(TextBook tb : tbs) {
				tbss.add(tb);
				tb.setPackageBook(pb);
//				new TextBookServiceImpl().update(tb);
			}
			pb.setTextBooks(tbss);
//			pb.setSemester(semester);
			service.add(pb);
			request.getRequestDispatcher("/pages/public/main.jsp").forward(request, response);
		}else if ("student".equals(request.getParameter("status"))) {
			String academicYear = request.getParameter("academicYear");
			String semester = request.getParameter("semester");
			String category = request.getParameter("category");
			String major = request.getParameter("major");
			String level = request.getParameter("level");
			String grade = request.getParameter("grade");
			
			String hql = "from PackageBook where academyYear='"+academicYear+"'"
					+" and semester='"+semester+"'"
					+" and category.id="+category
					+" and major.id="+major
					+" and level.id="+level
					+" and grade='"+grade+"'";
			PackageBook pb = (PackageBook) new PackageBookServiceImpl().queryByHql(hql);
			List<Object> blist = new ClassServiceImpl().list(BJi.class);
			request.setAttribute("blist", blist);
			request.setAttribute("pb", pb);
			request.getRequestDispatcher("/pages/storage/storage001/student/student.jsp").forward(request, response);
		}else if ("getstudent".equals(request.getParameter("status"))) {
			String bj = request.getParameter("bj");
			String num = request.getParameter("num");
			String hql = "from Student where num like '%"+num+"%'"
					+" and bJi.id="+bj;
			List<Student> students = new StudentServiceImpl().queryByHql(hql);
			request.setAttribute("pb", request.getParameter("pb"));
			request.setAttribute("students", students);
			request.getRequestDispatcher("/pages/storage/storage001/student/fetch.jsp").forward(request, response);
		}else if ("detail".equals(request.getParameter("status"))) {
			int id = Integer.valueOf(request.getParameter("id"));
			System.out.println("测试点++"+((PackageBook)service.get(id) ).getTextBooks().size());
			request.setAttribute("pb",(PackageBook)service.get(id) );
			request.getRequestDispatcher("/pages/storage/storage001/student/bookInfo_detil.jsp").forward(request, response);
		}
	}

}
